package org.lanqiao.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.lanqiao.entity.Admin;
import org.lanqiao.entity.Power;
import org.lanqiao.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource
	private AdminService adminService;
	@RequestMapping("/admin_add.do")
	public String admin_add() {
		return "admin/admin_add";
	}
	@RequestMapping("/admin_list.do")
	public ModelAndView admin_list(ModelAndView mav) {
		List<Admin> list= new ArrayList<Admin>();
		list=adminService.selectAllAdminService();
		//System.out.println(list);
		mav.setViewName("admin/admin_list");
		mav.addObject("list", list);
		return mav; 
	}
	@RequestMapping("/deleteAdmin.do")
	public String deleteAdmin(ModelAndView mav,int aid) throws Exception {
		boolean b=adminService.deleteAdmin(aid);
		
		//mav.addObject("list", list);
		return "redirect:admin_list.do"; 
	}
	@RequestMapping("/insertAdmin.do")
	public String insertAdmin(HttpServletRequest request)  {
		Admin admin = new Admin();
		//System.out.println(admin);
		admin.setAaccount(request.getParameter("aaccount"));
		admin.setAname(request.getParameter("aname"));
		admin.setApsw(request.getParameter("apsw"));
		admin.setAtel(request.getParameter("atel"));
		admin.setAemail(request.getParameter("aemail"));
		String[] power1=request.getParameterValues("power");
		Power[] power=new Power[power1.length];
		for(int i=0;i<power1.length;i++) {
			power[i]=new Power();
		}
		for(int i=0;i<power1.length;i++) {
			power[i].setPid(Integer.parseInt(power1[i]));
		}
		admin.setPower(Arrays.asList(power));
		//System.out.println(admin);
		boolean b = adminService.insertAdmin(admin);
		//System.out.println(b);
		//mav.addObject("list", list);
		return "redirect:admin_list.do"; 
	}
	
	/**
	 * 跳到修改页面
	 */
	@RequestMapping("/admin_modi.do")
	public ModelAndView admin_modi(ModelAndView mav,Admin admin1) {
		System.out.println(admin1);
		Admin admin=adminService.Login(admin1);
		System.out.println(admin);
		mav.setViewName("admin/admin_modi");
		mav.addObject("Admin", admin);
		return mav;
	}
	
	
}
